package ejemplo;

import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.regex.Pattern;

public class Search extends HttpServlet {

 private AccesoDatos ad;
 private ResultSet rs;
 private MyTableModel mtb;
 private String mensaje;
   
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {} 

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {} 

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
   RequestDispatcher rd;
   ServletContext context = getServletContext();
   InputStream header = context.getResourceAsStream("/header.xhtml");
   response.setContentType("text/html;charset=UTF-8");
   PrintWriter out = response.getWriter();
   String table=request.getParameter("table");
   try {
     if(header != null) {
       InputStreamReader isr = new InputStreamReader(header);
       BufferedReader reader = new BufferedReader(isr);
       String line ="";
       if(table.equals("libros")){
         while ((line = reader.readLine()) != null){
           if(Pattern.matches(".*OPER.*", line))
             out.println(line.replaceFirst("OPER", "Buscar libro"));
           else out.println(line);
         }
       } //endif table == libros
       else if(table.equals("temas")){
         while ((line = reader.readLine()) != null){
           if(Pattern.matches(".*OPER.*", line))
             out.println(line.replaceFirst("OPER", "Buscar tema"));
           else out.println(line);
         }
       } //endif table == temas
     } //endif if (header != null)

     // consulta y cuerpo
     if(table.equals("libros")){
       ad = new AccesoDatos("gencod");
       rs = ad.getAllTemas();
       mtb = new MyTableModel(rs);

       //leer plantilla
       InputStream abmc_libro = context.getResourceAsStream("/abmc-libro.xhtml");
       if(abmc_libro != null){
         InputStreamReader isr = new InputStreamReader(abmc_libro);
         BufferedReader reader = new BufferedReader(isr);
         String line ="";
         while ((line = reader.readLine()) != null){ //
           if(Pattern.matches(".*ACTION.*", line)){
             out.println(line.replaceFirst("ACTION", "\"./select\""));
             out.println("<input type=Hidden name=referer value=\"search\"/>");
           }
           else if(Pattern.matches(".*VALUE1.*", line))
             out.println(line.replaceFirst("VALUE1", "\"\""));
           else if(Pattern.matches(".*VALUE2.*", line))
             out.println(line.replaceFirst("VALUE2", "\"\""));
           else if(Pattern.matches(".*VALUE3.*", line))
             out.println(line.replaceFirst("VALUE3", "\"\""));
           else if(Pattern.matches(".*VALUE4.*", line))
             out.println(line.replaceFirst("VALUE4", "\"\""));
           else if(Pattern.matches(".*SELECT.*", line)){
             out.println(line.replaceFirst("SELECT", ""));
             out.println("<option value=\"99999\">Todos</option>");
             for (int row = 0; row < mtb.getRowCount(); row++){
                out.print("<option value=\"");
                out.print(mtb.getValueAt(row, 0));
                out.println("\">" + mtb.getValueAt(row, 1) + "</option>");
             } //endfor
           } //endif MATCHES SELECT           
           else if(Pattern.matches(".*BUTTON1.*", line))
             out.println(line.replaceFirst("BUTTON1", "\"Buscar\""));
           else if(Pattern.matches(".*BACK.*", line))
             out.println(line.replaceFirst("BACK", "\"./menu\""));
           else out.println(line);
         } //endwhile
       } //endif if (abmc_libro != null)
     } //endif(table.equals("libros")){
     else if(table.equals("temas")){
       //leer plantilla
       InputStream abmc_tema = context.getResourceAsStream("/abmc-tema.xhtml");
       if (abmc_tema != null) {
         InputStreamReader isr = new InputStreamReader(abmc_tema);
         BufferedReader reader = new BufferedReader(isr);
         String line ="";
         while ((line = reader.readLine()) != null){
           if(Pattern.matches(".*ACTION.*", line)){
             out.println(line.replaceFirst("ACTION", "\"./select\""));
             out.println("<input type=Hidden name=referer value=\"search\"/>");
           }
           else if(Pattern.matches(".*VALUE1.*", line))
             out.println(line.replaceFirst("VALUE1", "\"\""));
           else if(Pattern.matches(".*BUTTON1.*", line))
             out.println(line.replaceFirst("BUTTON1", "\"Buscar\""));
           else if(Pattern.matches(".*BACK.*", line))
             out.println(line.replaceFirst("BACK", "\"./menu\""));
           else out.println(line);
         } //endwhile
       } //endif if (abmc_tema != null)
     } //endif(table.equals("temas"))
   } catch(ClassNotFoundException cnfe){
     rd=request.getRequestDispatcher("/index");
     mensaje="Error fatal. Excepcion de clase no encontrada: " + cnfe.getMessage();
     request.setAttribute("mensaje",mensaje);
     rd.forward(request,response);
   } catch(SQLException sqle){
     rd=request.getRequestDispatcher("/index");
     mensaje="Error fatal. Excepcion de SQL: " + sqle.getErrorCode();
     request.setAttribute("mensaje",mensaje);
     rd.forward(request,response);
   } 
   finally { out.close(); }
 }

 @Override
 public String getServletInfo(){ return "Short description"; }
}